package com.LeeCode;

import java.util.HashMap;

/**
 * 统计好子数组的数目
 */

public class Code2537 {
    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 1, 1};
        int k = 10;
        System.out.println(new Code2537().countGood(nums, k));
    }

    public long countGood(int[] nums, int k) {
        long res = 0;
        int left, right = 0, count = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (left = 0; left < nums.length; left++) {
            while (count < k && right < nums.length) {
                count += map.getOrDefault(nums[right], 0);
                map.put(nums[right], map.getOrDefault(nums[right], 0) + 1);
                right++;
            }
            if (count >= k) {
                res += nums.length - right + 1;
            }
            map.put(nums[left], map.getOrDefault(nums[left], 0) - 1);
            count -= map.getOrDefault(nums[left], 0);
        }
        return res;
    }
}
